//
//  CustomDialogueController.swift
//  fbapp-ios
//
//  Created by 杨鸿 on 2020/8/20.
//  Copyright © 2020 fbapp-ios. All rights reserved.
//

import UIKit
import SnapKit

class CustomDialogueController: UIViewController {

    private let backdropView = UIView()
    private let windowView = UIView()
    private let shadowView = UIView()
    

    // MARK: - Properties
    
    var backdropAlpha: CGFloat {
        get {
            return self.backdropView.alpha
        }
        set {
            self.backdropView.alpha = newValue
        }
    }
    
    var backdropColor: UIColor = .black {
        didSet {
            self.backdropView.backgroundColor = self.backdropColor
        }
    }
    
    var cornerRadius: CGFloat {
        get {
            return self.windowView.layer.cornerRadius
        }
        set {
            self.windowView.layer.cornerRadius = newValue
            self.shadowView.layer.cornerRadius = newValue
        }
    }
    
    var borderColor: UIColor? {
        get {
            if let color = self.windowView.layer.borderColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            self.windowView.layer.borderColor = newValue?.cgColor
        }
    }
    
    // MARK: - View Lifecycle
    
    deinit {
//        LOG.V()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        self.shadowView.layer.shadowPath = UIBezierPath(rect: shadowView.bounds.insetBy(dx: 4, dy: 4)).cgPath
    }
    
    // MARK: - Initializer

    static func create(with view: UIView) -> CustomDialogueController {
        
        let vc = CustomDialogueController()
        vc.setup(with: view)
        return vc
    }
    
    // MARK: - Public Interface
    
    private func applyDropShadow(with color: UIColor, radius: CGFloat, offset: CGSize = CGSize.zero, alpha: Float = 1.0) {

        self.shadowView.layer.shadowColor = color.cgColor
        self.shadowView.layer.shadowOffset = offset
        self.shadowView.layer.shadowRadius = radius
        self.shadowView.layer.shadowOpacity = alpha
        self.shadowView.clipsToBounds = false
        self.shadowView.layer.masksToBounds = false
        self.shadowView.layer.shadowPath = UIBezierPath(rect: shadowView.bounds).cgPath
    }
    
    // MARK: - Private Interface

    private func setup(with customView: UIView) {
        
        self.view.addSubview(backdropView)
        self.view.addSubview(shadowView)
        self.view.addSubview(windowView)
        self.windowView.addSubview(customView)
        self.view.bringSubviewToFront(windowView)
        
        self.modalTransitionStyle = .crossDissolve
        self.modalPresentationStyle = .overCurrentContext
        self.view.backgroundColor = .clear
        self.cornerRadius = 10
        self.backdropAlpha = 0.7
        self.backdropColor = .black
        self.windowView.backgroundColor = .white
        self.applyDropShadow(with: .black, radius: 6, alpha: 0.7)
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapSelect))
        backdropView.addGestureRecognizer(tapGesture)
        
        backdropView.snp.makeConstraints { (maker) in
            maker.edges.equalToSuperview()
        }
        
        windowView.snp.makeConstraints { (maker) in
            maker.center.equalToSuperview()
            maker.width.equalTo(customView.frame.size.width)
            maker.height.equalTo(customView.frame.size.height)
        }
        
        shadowView.snp.makeConstraints { (maker) in
            maker.edges.equalTo(windowView)
        }
    }
    
    @objc func didTapSelect() {
        
        self.dismiss(animated: true, completion: nil)
    }
}

